home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / MIMETAG.PY < prev    next >
Encoding:
Python Source  |  2000-05-10  |  9.6 KB  |  253 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __rcs_id__='$Id: MIMETag.py,v 1.5 2000/05/10 18:43:34 tseaver Exp $'
  65. __version__='$Revision: 1.5 $'[11:-2]
  66.  
  67. from DocumentTemplate.DT_Util import *
  68. from DocumentTemplate.DT_String import String
  69. from MimeWriter import MimeWriter
  70. from cStringIO import StringIO
  71. import string, mimetools
  72.  
  73. MIMEError = "MIME Tag Error"
  74.  
  75. class MIMETag:
  76.     '''
  77.     '''
  78.  
  79.     name='mime'
  80.     blockContinuations=('boundary',)
  81.     encode=None
  82.  
  83.     def __init__(self, blocks):
  84.         self.sections = []
  85.  
  86.         for tname, args, section in blocks:
  87.             if tname == 'mime':
  88.                 args = parse_params( args
  89.                                    , type=None, type_expr=None
  90.                                    , disposition=None, disposition_expr=None
  91.                                    , encode=None, encode_expr=None
  92.                                    , name=None, name_expr=None
  93.                                    , filename=None, filename_expr=None
  94.                                    , skip_expr=None
  95.                                    , multipart=None
  96.                                    )
  97.                 self.multipart = args.get('multipart', 'mixed')
  98.             else:
  99.                 args = parse_params( args
  100.                                    , type=None, type_expr=None
  101.                                    , disposition=None, disposition_expr=None
  102.                                    , encode=None, encode_expr=None
  103.                                    , name=None, name_expr=None
  104.                                    , filename=None, filename_expr=None
  105.                                    , skip_expr=None
  106.                                    )
  107.  
  108.             has_key=args.has_key
  109.  
  110.             if has_key('type'): 
  111.                 type = args['type']
  112.             else:
  113.                 type = 'application/octet-stream'
  114.  
  115.             if has_key('type_expr'):
  116.                 if has_key('type'):
  117.                     raise ParseError, _tm('type and type_expr given', 'mime')
  118.                 args['type_expr']=VSEval.Eval(args['type_expr'], expr_globals)
  119.             elif not has_key('type'):
  120.                 args['type']='application/octet-stream'
  121.  
  122.             if has_key('disposition_expr'):
  123.                 if has_key('disposition'):
  124.                     raise ParseError, _tm('disposition and disposition_expr given', 'mime')
  125.                 args['disposition_expr']=VSEval.Eval(args['disposition_expr'], expr_globals)
  126.             elif not has_key('disposition'):
  127.                 args['disposition']=''
  128.  
  129.             if has_key('encode_expr'):
  130.                 if has_key('encode'):
  131.                     raise ParseError, _tm('encode and encode_expr given', 'mime')
  132.                 args['encode_expr']=VSEval.Eval(args['encode_expr'], expr_globals)
  133.             elif not has_key('encode'):
  134.                 args['encode']='base64'
  135.  
  136.             if has_key('name_expr'):
  137.                 if has_key('name'):
  138.                     raise ParseError, _tm('name and name_expr given', 'mime')
  139.                 args['name_expr']=VSEval.Eval(args['name_expr'], expr_globals)
  140.             elif not has_key('name'):
  141.                 args['name']=''
  142.  
  143.             if has_key('filename_expr'):
  144.                 if has_key('filename'):
  145.                     raise ParseError, _tm('filename and filename_expr given', 'mime')
  146.                 args['filename_expr']=VSEval.Eval(args['filename_expr'], expr_globals)
  147.             elif not has_key('filename'):
  148.                 args['filename']=''
  149.  
  150.             if has_key('skip_expr'):
  151.                 args['skip_expr']=VSEval.Eval(args['skip_expr'], expr_globals)
  152.  
  153.             if args['encode'] not in \
  154.             ('base64', 'quoted-printable', 'uuencode', 'x-uuencode',
  155.              'uue', 'x-uue', '7bit'):
  156.                 raise MIMEError, (
  157.                     'An unsupported encoding was specified in tag')
  158.  
  159.             self.sections.append((args, section.blocks))
  160.  
  161.  
  162.     def render(self, md):
  163.         contents=[]
  164.         mw = MimeWriter(StringIO())
  165.         outer = mw.startmultipartbody(self.multipart)
  166.         for x in self.sections:
  167.             a, b = x
  168.             has_key=a.has_key
  169.  
  170.             if has_key('skip_expr') and a['skip_expr'].eval(md):
  171.                 continue
  172.                 
  173.             inner = mw.nextpart()
  174.  
  175.             if has_key('type_expr'): t=a['type_expr'].eval(md)
  176.             else: t=a['type']
  177.  
  178.             if has_key('disposition_expr'): d=a['disposition_expr'].eval(md)
  179.             else: d=a['disposition']
  180.  
  181.             if has_key('encode_expr'): e=a['encode_expr'].eval(md)
  182.             else: e=a['encode']
  183.  
  184.             if has_key('name_expr'): n=a['name_expr'].eval(md)
  185.             else: n=a['name']
  186.             
  187.             if has_key('filename_expr'): f=a['filename_expr'].eval(md)
  188.             else: f=a['filename']
  189.  
  190.             if d:
  191.                 if f:
  192.                     inner.addheader('Content-Disposition', '%s;\n filename="%s"' % (d, f))
  193.                 else:
  194.                     inner.addheader('Content-Disposition', d)
  195.  
  196.             inner.addheader('Content-Transfer-Encoding', e)
  197.             if n:
  198.                 plist = [('name', n)]
  199.             else:
  200.                 plist = []
  201.  
  202.             innerfile = inner.startbody(t, plist, 1)
  203.  
  204.             output = StringIO()
  205.             if e == '7bit':
  206.                 innerfile.write(render_blocks(b, md))
  207.             else:
  208.                 mimetools.encode(StringIO(render_blocks(b, md)),
  209.                                  output, e)
  210.                 output.seek(0)
  211.                 innerfile.write(output.read())
  212.  
  213.         # XXX what if self.sections is empty ??? does it matter that mw.lastpart() is called
  214.         # right after mw.startmultipartbody() ?
  215.         if x is self.sections[-1]:
  216.             mw.lastpart()
  217.  
  218.         outer.seek(0)
  219.         return outer.read()
  220.  
  221.  
  222.     __call__=render
  223.  
  224.  
  225.  
  226. String.commands['mime'] = MIMETag
  227.  
  228.  
  229.  
  230.  
  231.  
  232.